package com.azt.provider.impl;

import com.azt.api.pojo.Hotproduct;
import com.azt.api.pojo.Newpub;
import com.azt.api.pojo.OldProduct;
import com.azt.api.service.HotproductService;
import com.azt.model.page.Pagination;
import com.azt.provider.mapper.HotproductMapper;
import com.azt.provider.mapper.NewpubMapper;
import com.azt.provider.mapper.OldProductMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class HotproductServiceImpl  implements HotproductService {

	@Autowired
	private HotproductMapper hotproductMapper;

	@Autowired
	private NewpubMapper newpubMapper;

	@Autowired
	private OldProductMapper productMapper;

	@Override
	public Pagination getHotproductPage(int pageno, int pagesize, Map<String, String> searchMap) {
		Page page = PageHelper.startPage(pageno, pagesize);
		Example example = new Example(Hotproduct.class);
		example.orderBy("seq").asc().orderBy("updatetime").desc();
		List<Hotproduct> list = hotproductMapper.selectByExample(example);

		Pagination p = new Pagination(page, list);
		p.setList(list);
		p.setParams(searchMap);
		return p;
	}

	@Override
	public List<Hotproduct> queryListHotprocuts() {
		PageHelper.startPage(1, 10);
		return hotproductMapper.selectAll();
	}

	@Override
	public Pagination getHotproductBysql(int pageNo, int pageSize, Map<String, String> searchMap) {
		Page<Object> startPage = PageHelper.startPage(pageNo, pageSize);
		List<Hotproduct> list = hotproductMapper.selectAll();
		Pagination p = new Pagination(startPage, list);
		p.setList(list);
		p.setParams(searchMap);
		return p;
	}

	@Override
	public Integer delHotproduct(Integer hotid) {
		Integer result=1;
		try {
			Hotproduct hotproduct = new Hotproduct();
			hotproduct.setId(hotid);
			hotproductMapper.deleteByPrimaryKey(hotproduct);
		} catch (Exception e) {
			result=0;
		}
		return result;
	}

	@Override
	public Integer changeHotproEnable(Integer id, Integer enable) {
		Integer result = 1;
		try {
			Hotproduct hotproduct = new Hotproduct();
			hotproduct.setId(id);
			if (enable == 0) {
				hotproduct.setIsabled(1);
			} else {
				hotproduct.setIsabled(0);
			}
			hotproductMapper.updateByPrimaryKeySelective(hotproduct);
		} catch (Exception e) {
			result = 0;
		}
		return result;
	}

	@Override
	public List<Newpub> getNewproByname(Integer pageindex, Integer pagenum, String name) {
		PageHelper.startPage(pageindex, pagenum);
		Example example = new Example(Newpub.class);
		Example.Criteria criteria = example.createCriteria().andEqualTo("newpubstate", 1);
		if(StringUtils.isNotBlank(name)) {
			criteria.andLike("productname", name);
		}
		return newpubMapper.selectByExample(example);
	}

	@Override
	public void insertNewHotpro(Hotproduct pro) {
		hotproductMapper.insert(pro);
	}

	@Override
	public void updateHotpro(Hotproduct pro) {
		hotproductMapper.updateByPrimaryKey(pro);
	}

	@Override
	public Hotproduct getHotproductByid(Integer hotid) {
		return hotproductMapper.selectByPrimaryKey(hotid);
	}

	@Override
	public List<OldProduct> getProBynameOrid(Integer pageindex, Integer pagenum, String name) {
		PageHelper.startPage(pageindex, pagenum);

		Example example = new Example(OldProduct.class);
		if(StringUtils.isNotBlank(name)) {
			example.createCriteria().andLike("name", name);
		}
		return productMapper.selectByExample(example);
	}
}
